import {setJsonValue} from "@/utils/ywuCommon";
import {getCommonParamInfo} from "@/utils/commonParamUtil";
import request from "@/utils/request";
import jsonpath from "jsonpath";

/**
 * 构建请求入参
 *
 * @param commonParam
 * @param paramList
 */
export function buildRequestParam(commonParam, paramList) {
    if (paramList.length === 1 && paramList[0].variableName === "$") {
        let requestParam = paramList[0]
        if (requestParam.type === "custom") {
            let resValue = ""
            if (requestParam.variableType === "String") {
                resValue = requestParam.variableValue
            } else {
                resValue = JSON.parse(requestParam.variableValue)
            }
            return resValue
        } else {
            return getCommonParamInfo(commonParam, requestParam.variableParamName)
        }
    }
    let buildRes = {}
    for (let i = 0; i < paramList.length; i++) {
        let requestParam = paramList[i]
        if (requestParam.type === "custom") {
            let resValue = ""
            if (requestParam.variableType === "String") {
                resValue = requestParam.variableValue
            } else {
                resValue = JSON.parse(requestParam.variableValue)
            }
            if (requestParam.variableName.startsWith("$.")) {
                setJsonValue(buildRes, requestParam.variableName, resValue)
            } else {
                buildRes[requestParam.variableName] = resValue
            }
        } else {
            let resValue = getCommonParamInfo(commonParam, requestParam.variableParamName)
            if (requestParam.variableName.startsWith("$.")) {
                setJsonValue(buildRes, requestParam.variableName, resValue)
            } else {
                buildRes[requestParam.variableName] = resValue
            }
        }
    }
    return buildRes
}

/**
 * SOA请求
 * @param buildRes
 * @param apiMethod
 * @param apiPath
 * @param apiResponseList
 * @param commonParam
 * @param execThen
 * @param execCatch
 * @param execParam
 * @constructor
 */
export function MSOARequest(buildRes, apiMethod, apiPath,
                            apiResponseList, commonParam, execThen, execCatch, execParam) {
    // 处理微服务自有API
    let methodType = "";
    if (apiMethod === 'GET') {
        methodType = "get";
    } else if (apiMethod === 'POST') {
        methodType = "post";
    }
    request({
        url: apiPath,
        method: methodType,
        data: buildRes
    }).then(res => {
        // 解析返回结果
        let responseList = apiResponseList;
        for (let i = 0; i < responseList.length; i++) {
            let responseParamInfo = responseList[i]
            // 通过jsonPath解析值
            let jsonPathResValue = jsonpath.query(res, responseParamInfo.variableValue)
            for (let j = 0; j < commonParam.length; j++) {
                let innerCommonParamInfo = commonParam[j]
                // 判断当前键值是否相同
                if (responseParamInfo.variableName === innerCommonParamInfo.variableName) {
                    let commonInfo = "";
                    // 根据全局变量类型进行判断要转换的数据
                    if (innerCommonParamInfo.variableType === 'String') {
                        commonInfo = jsonPathResValue[0]
                    } else {
                        commonInfo = jsonPathResValue
                    }
                    innerCommonParamInfo.variableValue = commonInfo;
                    break;
                }
            }
        }
        execThen(execParam)
    }).catch(res => {
        execCatch(execParam)
    })
}